<?php

namespace app\admin\validate\jeek\shop;

use think\Validate;

/**
 * SkuGoods商品插件
 */
class SkuGoods extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'category_id' => 'require',
        'name'        => 'require',
        'image'       => 'require',
        'images'      => 'require',
//        'details'     => 'require',
//        'skus'        => 'checkSkus',
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'category_id' => '分类不能为空',
        'name'        => '名称不能为空',
        'image'       => '封面不能为空',
        'images'      => '相册不能为空',
//        'details'     => '详情图不能为空',
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['category_id','name','image','images'],
        'edit' => ['category_id','name','image','images'],
    ];
    
    protected static function checkSkus($value, $rule, $data, $field = '')
    {
        $skus = json_decode($value,true);
        if(!count($skus)) return '服务类型不能为空';
        $result = true;
        foreach ($skus as $k => $v) {
            if(!($v['market_price'] && $v['market_price']>0 && $v['price'] && $v['price']>0)) {
                $result = '原价/现价，请填入合法值';
                break;
            }
        }
        return $result;
    }
}
